<?php

use km\util;
use km\util\InterceptingFilterChain;
use km\util\InterceptingFilter;
use \PHPUnit_Framework_TestCase;

require_once dirname(__FILE__) . '/../../lib/km/util/InterceptingFilter.php';
require_once dirname(__FILE__) . '/../../lib/km/util/InterceptingFilterChain.php';

class StubInterceptingFilter implements InterceptingFilter {
	private $calles = 0;
	function filter(InterceptingFilterChain $chain) {
		if (0 == $this->calles) {
			$this->calles++;
			$chain->next();
		} else {
			return 1;
		}
	}
	function getCallCount()
	{
		return $this->calles;
	}
}

class InterceptingFilterChainTest extends PHPUnit_Framework_TestCase {
	function testSingleItemChain()
	{
		$c = new InterceptingFilterChain();
		
		$item = new StubInterceptingFilter();
		$c->add($item);
		$c->run();
		
		$this->assertEquals(1, $item->getCallCount());
	}
	function testReturnValue()
	{
		$c = new InterceptingFilterChain();
		
		$item = new StubInterceptingFilter();
		$c->add($item);
		
		// first run should only call next() without return (@see StubInterceptingFilter#filter())
		$this->assertEquals(null, $c->run());
		// second and further runs should return 1
		$this->assertEquals(1, $c->run());
	}
}